var areaJson = [ "华东", "华南", "华北", "政企" ];

var signJson = [ "签到", "已签到" ];
var actType;// 1：新增活动，2：编辑活动
var editId;
var pagetotal;
var checkFlag = true;
$(function() {
	$("#activityId").change(changeFileParam);
	$(".pageSelect").change(changepageSelect);
	doSearch()
});

function changeFileParam() {
	$("#activityId").val()
	$(".pageSelect").val()
	doSearch()
}

function changepageSelect() {
	$("#pageNumber").val(1);
	$(".pageSelect").val()
	doSearch()
}

function index() {
	$("#pageNumber").val(1);
	doSearch()
}

function next() {
	var pagenum = $("#pageNumber").val();
	var pageSize = $.trim($(".pageSelect").val());
	if (pagenum < Math.ceil(pagetotal / pageSize)) {
		$("#pageNumber").val(parseInt(pagenum) + 1);
	}
	doSearch()
}

function previous() {
	var pagenum = $("#pageNumber").val();
	if (pagenum > 1) {
		$("#pageNumber").val(pagenum - 1);
	}
	doSearch()
}

function last() {
	var pageSize = $.trim($(".pageSelect").val());
	var num = Math.ceil(pagetotal / pageSize);
	$("#pageNumber").val(num);
	doSearch()
}

// 查询
function doSearch() {
	$(".lay3_tab1").empty();
	if (!checkParams()) {
		return;
	}
	var parameter = {};
	var activityId = $.trim($('#activityId').val());
	var userName = $.trim($("#userName").val());
	var pageSize = $.trim($(".pageSelect").val());
	var pageNumber = $.trim($("#pageNumber").val());
	parameter['activityId'] = activityId;
	parameter['userName'] = userName;
	parameter['pageSize'] = pageSize;
	parameter['pageNumber'] = pageNumber;
	url = '../view/findPage';
	$
			.post(
					url,
					parameter,
					function(rows) {
						for (var i = 0; i < rows.rows.length; i++) {
							$(".lay3_tab1")
									.append(
											"<tr>"
													+ "<td id='sign_userName_"
													+ [ i ]
													+ "'>"
													+ rows.rows[i].userName
													+ "</td>"
													+ "<td>"
													+ rows.rows[i].userLevel
													+ "</td>"
													+ "<td>"
													+ rows.rows[i].companyName
													+ "</td>"
													+ "<td>"
													+ areaJson[rows.rows[i].area - 1]
													+ "</td>"
													+ "<td class='blue_word' onclick='sign("+rows.rows[i].id+","+ i +")' id="+i+">"
													+ signJson[rows.rows[i].isSigned]
													+ "</td>"
													+ "<td><a href='#' id='file_btn_"+i+"' class='photo_icon'>上传照片</a>"
													+ "<a href='../questions/listQuestions?activityId="+rows.rows[i].activityId+"&companyUserId="+rows.rows[i].id+"' class='ask_icon'>提问</a></td>"
													+ "</tr>");
							uploadPic(i,rows.rows[i].id);
						}
						pagetotal = rows.total;
						$(".info").html(
								"<p>共" + rows.total + "条记录共"
										+ Math.ceil(pagetotal / pageSize)
										+ "页</p>");
					}, 'json');

}

function sign(uid,id) {
	console.log($("#sign_userName_0").val())
	var parameter = {};
	url = "../view/signIn";
	var falg = $("#"+id).text();
	parameter['id'] = uid;
	if (falg == "已签到") {
		$.myMessager.alert({
			'title' : '提示',
			'msg' : ' <p><strong>该客户已经签到，请勿重复签到！</strong></p>'
		});
	} else {
		$.post(url, parameter, function(result) {
			if (result.info) {
				$.myMessager.alert({
					'title' : '提示',
					'msg' : ' <p><strong>' + result.info + '</strong></p>'
				});
			} else if (result.result == 1) {
				$.myMessager.alert({
					'title' : '提示',
					'msg' : ' <p><strong>签到成功</strong></p>'
				});
				doSearch();
			}
		}, 'json');
	}
}

function emptyEditWin() {
	$('#area2').val('');
	$('#remark2').val('');
	$('#topic2').val('');
}
// 新增试题
function addUser() {
	if (!checkUser()) {
		return;
	}
	var parameter = {};
	var activityId = $.trim($('#activityId2').val());
	var userName = $.trim($("#userName2").val());
	var companyName = $.trim($("#companyName2").val());
	var userLevel = $.trim($("#userLevel2").val());
	var orderType = $.trim($("#orderType2").val());
	var expectSignTime = $.trim($("#expectSignTime2").val());
	var remark = $.trim($("#remark2").val());
	parameter['activityId'] = activityId;
	parameter['userName'] = userName;
	parameter['companyName'] = companyName;
	parameter['userLevel'] = userLevel;
	parameter['orderType'] = orderType;
	parameter['expectSignTime'] = expectSignTime;
	parameter['remark'] = remark;
	var url = '';
	// 新增试题
	if (actType == 1) {
		parameter['status'] = 0;
		url = '../companyUser/add';
		$.post(url, parameter, function(result) {
			if (result.info) {
				$.myMessager.alert({
					'title' : '提示',
					'msg' : ' <p><strong>' + result.input + '</strong></p>'
				});
			} else if (result.result) {
				$("#addWin").myWindow("close");
				emptyEditWin();
				$.myMessager.alert({
					'title' : '提示',
					'msg' : ' <p><strong>新增成功</strong></p>'
				});
				doSearch();
			} else {
				$.myMessager.alert({
					'title' : '提示',
					'msg' : ' <p><strong>未知错误！</strong></p>'
				});
			}
		}, 'json');
	} else if (actType == 2) {// 编辑试题
		url = '../companyUser/edit';
		parameter['id'] = editId;
		$.post(url, parameter, function(result) {
			if (result.info) {
				$.myMessager.alert({
					'title' : '提示',
					'msg' : ' <p><strong>' + result.input + '</strong></p>'
				});
			} else if (result.result) {
				$.myMessager.alert({
					'title' : '提示',
					'msg' : ' <p><strong>更新成功</strong></p>'
				});
				$("#addWin").myWindow("close");
				doSearch();
			}
		}, 'json');
	}

}

function checkUser() {
	var userName = $.trim($("#userName2").val());
	var companyName = $.trim($("#companyName2").val());
	var userLevel = $.trim($("#userLevel2").val());
	var remark = $.trim($("#remark2").val());

	if (userName == null || userName == '') {
		$.myMessager.alert({
			'title' : '提示',
			'msg' : ' <p><strong>客户名不能为空！</strong></p>'
		});
		return false;
	}
	if (companyName == null || companyName == '') {
		$.myMessager.alert({
			'title' : '提示',
			'msg' : ' <p><strong>公司名不能为空！</strong></p>'
		});
		return false;
	}
	if (userName != null && userName.length > 80) {
		$.myMessager.alert({
			'title' : '提示',
			'msg' : ' <p><strong>客户名长度不能大于80！</strong></p>'
		});
		return false;
	}
	if (companyName != null && companyName.length > 80) {
		$.myMessager.alert({
			'title' : '提示',
			'msg' : ' <p><strong>公司名长度不能大于80！</strong></p>'
		});
		return false;
	}
	if (userLevel != null && userLevel.length > 20) {
		$.myMessager.alert({
			'title' : '提示',
			'msg' : ' <p><strong>职位长度不能大于20！</strong></p>'
		});
		return false;
	}
	if (remark != null && remark.length > 80) {
		$.myMessager.alert({
			'title' : '提示',
			'msg' : ' <p><strong>备注长度不能大于80！</strong></p>'
		});
		return false;
	}
	return true;
}

// 去除输入框中的空格
function trimInput(jq) {
	jq.val($.trim(jq.val()));
}

//
function deleteUserWin(userId) {
	$.myMessager.confirm({
		'title' : '温馨提示',
		'msg' : '确定删除客户？'
	}, function(result) {
		if (result) {
			deleteUser(userId);
		}
	});
}
function doReset() {
	document.getElementById("searchForm").reset();
}

function onChangeDate() {
	if ($("#startTime").val() || $("#endTime").val()) {
		old_startTime = $("#startTime").val();
		old_endTime = $("#endTime").val();
	}
	if ($("#base_1").get(0).selectedIndex != 0) {
		document.getElementById("startTime").value = "";
		document.getElementById("endTime").value = "";
		checkFlag = false;
		document.getElementById("checkDateFlag").value = 1;
	} else {
		document.getElementById("startTime").value = old_startTime;
		document.getElementById("endTime").value = old_endTime;
		checkFlag = true;
		document.getElementById("checkDateFlag").value = 0;
	}
}

function checkParams() {
	if (!checkFlag) {
		return true;
	}
	// if(dateDiff($("#startTime").val(),$("#endTime").val())>31) {
	// $.myMessager.alert({'title':'提示','msg':'
	// <p><strong>时间间隔不能超过一个月！</strong></p>'});
	// return false;
	// }
	return true;
}